#!/usr/bin/env ruby

require 'json'
require 'rest-client'

LKP_SRC = ENV['LKP_SRC'] || File.direname(File.dirname(File.realpath($PROGRAM_NAME)))

if ENV['RESULT_ROOT']
    fn = File.join ENV['RESULT_ROOT'], 'eulermaker_build_image_result'
elsif ARGV[0]
    fn = ARGV[0]
end

stats_json = File.read(fn)
stats = JSON::parse(stats_json)

puts ".result.pipeline_id: #{stats['pipeline_id']}"
puts ".result.start_time: #{stats['start_time']}"
puts ".result.end_time: #{stats['end_time']}"
puts ".result.status: #{stats['status']}"
puts ".result.log_link: https://eulermaker.compass-ci.openeuler.openatom.cn/api#{stats['log_link']}"
puts ".result.iso_type: #{stats['image_config']['release_image_config']['iso_type']}"

arch = stats['pipeline_info']['arch']
category = stats['pipeline_info']['category']
image_format = stats['pipeline_info']['image_format']
product = stats['image_config']['release_image_config']['product']

puts ".result.arch: #{arch}"
puts ".result.category: #{category}"
puts ".result.image_format: #{image_format}"
puts ".result.product: #{product}"

version = stats['image_config']['release_image_config']['version']
release = stats['image_config']['release_image_config']['release']

if !release.nil? && !release.empty?
    version = "#{version}-#{release}"
end

puts ".result.version: #{version}"

image_link_prefix = "https://eulermaker.compass-ci.openeuler.openatom.cn/api#{stats['image_link']}"
if category == "tailor"
    case image_format
    when "iso"
        # 暂不可用，貌似可以不存在version
        image_link = "#{image_link_prefix}/ISO/#{arch}/#{product}-#{version}-#{arch}.iso"
    when "iso_normal"
        image_link = "#{image_link_prefix}/ISO/#{arch}/#{product}-#{version}-#{arch}-dvd.iso"
    when "qcow2"
        image_link = "#{image_link_prefix}/#{product}-#{version}-#{arch}.#{image_format}.xz"
    when "docker"
        # 暂时不可用，貌似可以不存在version
        image_link = "#{image_link_prefix}/#{product}-#{version}.#{arch}.tar.xz"
    when "mini_docker"
        # 暂时不可用，貌似可以不存在version和架构
        image_link = "#{image_link_prefix}/#{product}-#{version}.tar"
    when "cpio"
        # 暂时不可用，实际文件名中带有未知来源的时间拼接
        image_link = "#{image_link_prefix}/#{product}-#{version}-#{arch}.rootfs.cpio.gz"
    when "mini_cpio"
        # 暂时不可用，mini_cpio貌似不存在架构之分
        image_link = "#{image_link_prefix}/#{product}-#{version}-#{arch}.cpio.gz"
    when "raspi"
        image_link = "#{image_link_prefix}/#{product}-#{version}-raspi-#{arch}.img.xz"
    end

elsif category == "standard"
    case image_format
    when "iso"
        image_link = "#{image_link_prefix}/ISO/#{arch}/#{product}-#{version}-#{arch}-dvd.iso"
    when "qcow2"
        image_link = "#{image_link_prefix}/virtual_machine_img/#{arch}/#{product}-#{version}-#{arch}.#{image_format}.xz"
    when "docker"
        image_link = "#{image_link_prefix}/docker_img/#{arch}/#{product}-#{version}.#{arch}.tar.xz"
    when "stratovirt"
        image_link = "#{image_link_prefix}/stratovirt_img/#{arch}/#{product}-#{version}-stratovirt-#{arch}.img.xz"
    end
end

puts ".result.image_link: #{image_link}"
